Comprehensive Git and GitHub Reference

Table of Contents


Git Basics

Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files.

Key Concepts


Configuring Git

Proper configuration is essential for using Git effectively.

# Set your global username and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Check your current configuration
git config --list

# Set your default editor
git config --global core.editor "vim"

# Set the default branch name
git config --global init.defaultBranch main

# Enable colorful output
git config --global color.ui auto

Working with Repositories

# Initialize a new Git repository
git init

# Clone an existing repository from a URL
git clone <repository-url>

# Check the status of your repository
git status

# Show a more compact status
git status -s

# Display all ignored files
git status --ignored

Staging and Committing

Prepare and save your changes to the repository's history.

# Stage a specific file for the next commit
git add <filename>

# Stage all changes in the current directory
git add .

# Interactively stage parts of a file
git add -p

# Commit the staged changes with a message
git commit -m "Your descriptive commit message"

# Stage all tracked files and commit in one step
git commit -a -m "Your message"

# Amend the last commit (change message or add files)
git commit --amend

# Remove a file from the staging area but keep it in the working directory
git rm --cached <filename>

# Preview files that would be removed
git rm -r -n <directory>

# Stop tracking a file that was previously committed
git rm --cached <filename>

Branching and Merging

Manage parallel lines of development and integrate them.

# List all local branches
git branch

# List all remote branches
git branch -r

# List all local and remote branches
git branch -a

# Create a new branch
git branch <branch-name>

# Delete a branch (use -D to force delete)
git branch -d <branch-name>

# Switch to a different branch
git checkout <branch-name>

# Create a new branch and switch to it
git checkout -b <branch-name>

# Merge a branch into your current branch
git merge <branch-name>

# Abort a merge in case of conflicts
git merge --abort

# Resolve merge conflicts manually, then:
git add <conflicted-file>
git commit

Undoing Changes

Correct mistakes and revert unwanted changes.

# Unstage a file
git reset <file>

# Revert changes to a file in the working directory
git checkout -- <file>

# Create a new commit that undoes the changes from a specific commit
git revert <commit-hash>

# Reset to a previous commit, discarding all changes since
git reset --hard <commit-hash>

# Reset to a previous commit, keeping changes unstaged
git reset --soft HEAD~1

# Restore a file to a specific commit's version
git restore --source=<commit-hash> <file>

Working with Remotes

Collaborate with others by sharing your changes.

# Add a new remote repository
git remote add <remote-name> <url>

# List all remotes
git remote -v

# Fetch changes from a remote
git fetch <remote-name>

# Pull changes from a remote branch and merge them
git pull <remote-name> <branch-name>

# Push your changes to a remote branch
git push <remote-name> <branch-name>

# Force push (use with caution!)
git push <remote-name> <branch-name> --force

# Set an upstream branch for your local branch
git push -u <remote-name> <branch-name>

Advanced Git

Rebasing

Rebasing is an alternative to merging for integrating changes from one branch to another. It rewrites the commit history to create a linear sequence of commits.

# Rebase the current branch onto another branch
git rebase <base-branch>

# Start an interactive rebase to edit, squash, or reorder commits
git rebase -i HEAD~<number-of-commits>

# Continue a rebase after resolving conflicts
git rebase --continue

# Abort a rebase
git rebase --abort

Cherry-Picking

Apply a specific commit from one branch to another.

# Apply a single commit to the current branch
git cherry-pick <commit-hash>

Stashing

Temporarily save changes that are not ready to be committed.

# Stash your current changes
git stash

# List all stashed changes
git stash list

# Apply the most recent stash and remove it from the list
git stash pop

# Apply a specific stash
git stash apply stash@{2}

# Clear all stashes
git stash clear

Git Workflows

Different strategies for managing branches and collaboration.


GitHub Collaboration

Leveraging GitHub for team-based development.

# Fork a repository (done on the GitHub UI)

# Clone your fork
git clone <your-fork-url>

# Add the original repository as an upstream remote
git remote add upstream <original-repo-url>

# Fetch changes from the upstream repository
git fetch upstream

# Merge changes from upstream/main into your local main
git merge upstream/main

# Keep your fork's main branch up-to-date
git pull upstream main

Common Git Commands

Command Description
git log Show commit history
git log --oneline --graph Show a condensed, graphical log
git diff Show changes between commits, branches, or files
git diff --staged Show changes that are staged for the next commit
git tag Create, list, or delete tags for specific commits
git blame <file> Show who last modified each line of a file
git show <commit-hash> Show details of a specific commit
git reflog View a log of all changes to branches and HEAD

Tips and Best Practices

Read More